﻿
using PiFramework;
using PiUi.View;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace PiUi.Froms
{
    public partial class HomeView : BaseView
    {

        private string jsPath = AppDomain.CurrentDomain.BaseDirectory + "js\\js.PiCode";
        public HomeView()
        {
            InitializeComponent();
            //PNotificationCenter.DefaultCenter().AddObserver("PLoger", this.OnNotification);
            //PNotificationCenter.DefaultCenter().AddObserver("sendImage", this.OnNotification);
            //WorkThread = new PJintWorkThread();
            //PJintThreadControl.Instance.AddThread(WorkThread, "work1");
            //Console.WriteLine(jsPath);




        }

        public override void LoadTheme() {

            foreach (Control btns in this.groupBox2.Controls)
            {
                if (btns.GetType() == typeof(Button))
                {
                    Button btn = (Button)btns;
                    btn.BackColor = ThemeColor.PrimaryColor;
                    btn.ForeColor = Color.White;
                    btn.FlatAppearance.BorderColor = ThemeColor.SecondaryColor;
                }
            }
            this.groupBox1.ForeColor = ThemeColor.SecondaryColor;
            this.groupBox2.ForeColor = ThemeColor.SecondaryColor;
            this.groupBoxLog.ForeColor = ThemeColor.SecondaryColor;
            this.labelResImage.ForeColor = ThemeColor.PrimaryColor;
            this.labelSouImage.ForeColor = ThemeColor.PrimaryColor;
            piScrollbar1.SetscrollbarMouseUp(ThemeColor.SecondaryColor);
            piScrollbar1.SetscrollbarMouseEnter(ThemeColor.PrimaryColor);
            piScrollbar1.SetscrollbarMouseLeave(ThemeColor.PrimaryColor);

            this.comboBox1.ForeColor = ThemeColor.PrimaryColor;

    




        }

 

        private void myFrom_Load(object sender, EventArgs e)
        {
     

            LoadTheme();
            //WorkThread.ReadJSCode(jsPath);
            //WorkThread.addPlugin();
            // 初始化 RichTextBox
            richTextBoxLoger.Dock = DockStyle.Fill;
            richTextBoxLoger.Multiline = true;
            richTextBoxLoger.ScrollBars = RichTextBoxScrollBars.Vertical;
            piScrollbar1.WinH = this.Height;			//传入窗口的高度
            piScrollbar1.control = panel1;
            piScrollbar1.SetscrollbarMouseUp(ThemeColor.SecondaryColor);
            piScrollbar1.SetscrollbarMouseEnter(ThemeColor.PrimaryColor);
            piScrollbar1.SetscrollbarMouseLeave(ThemeColor.PrimaryColor);



        }
        private const int MaxLogLines = 1000; // 设置最大日志行数
        Color color = Color.Black;
        public void OnNotification(string name, object sender, Dictionary<string, object> userInfo)
        {
            if (this.IsHandleCreated)
            {
                Invoke(new Action(() =>
                {
                    if (name == "PLoger")
                    {
                        string message = userInfo["message"].ToString();
                        string level = userInfo["level"].ToString();
                

                        if (level == "INFO")
                        {
                            color = Color.Blue;

                        }
                        else if (level == "WARNING")
                        {
                            color = Color.DarkOrange;

                        }else if (level == "ERROR")
                        {
                            color = Color.Red;
                        }
                        // 使用 AppendText 添加日志信息
                        richTextBoxLoger.AppendText(message + Environment.NewLine);
                        richTextBoxLoger.SelectionColor = color;
                        // 限制日志的最大行数，超过时只保留最新的 MaxLogLines 行
                        if (richTextBoxLoger.Lines.Length > MaxLogLines)
                        {
                            var lines = richTextBoxLoger.Lines;
                            // 从后面截取最新的 MaxLogLines 行
                            var newLines = new string[MaxLogLines];
                            Array.Copy(lines, lines.Length - MaxLogLines, newLines, 0, MaxLogLines);
                            richTextBoxLoger.Lines = newLines;
                        }
                        // 将光标移动到文本末尾，使滚动条保持在底部
                        richTextBoxLoger.SelectionStart = richTextBoxLoger.Text.Length;
                        richTextBoxLoger.ScrollToCaret();
                    }

                    if (name == "sendImage")
                    {
                        if (userInfo.ContainsKey("souImage") && userInfo.ContainsKey("resImage"))
                        {
                            string base64StringSou = userInfo["souImage"].ToString();  // 取出 Base64 图像
                            string base64StringRes = userInfo["resImage"].ToString();  // 取出 Base64 图像
                            //// **Base64 转换回 Bitmap**
                            Bitmap imageSou = ConvertBase64ToBitmap(base64StringSou);
                            Bitmap imageRes = ConvertBase64ToBitmap(base64StringRes);
                            this.ShowImage1.Image = imageSou;
                            this.ShowImage2.Image = imageRes;
                        }
                    }
                }));
            }
        }
       
        
        private Bitmap ConvertBase64ToBitmap(string base64)
        {
            base64 = base64.Replace("data:image/png;base64,", ""); // 移除前缀
            byte[] imageBytes = Convert.FromBase64String(base64);
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(imageBytes))
            {
                return new Bitmap(ms);
            }
        }



        private void buttonRun_Click(object sender, EventArgs e)
        {
            //WorkThread.ReadJSCode(jsPath);
            //int m_ThreadSpeed = (int)PSysConfigVar.Instance.GetConfigVar("ThreadSpeed");
            //PJintThreadControl.Instance.AllThreadRun(m_ThreadSpeed);

        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            PJintThreadControl.Instance.AllThreadStop();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
